<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="javax.jdo.Query" %>
<%@ page import="java.text.*" %>
<%@ page import="appnode.Node" %>
<%@ page import="appnode.Ratings" %>
<%@ page import="appnode.RateUtils" %>
<%@ page import="appnode.SearchNode" %>
<%@ page import="appnode.Link" %>
<%@ page import="appnode.Linktools" %>
<%@ page import="appnode.Interface" %>
<%@ page import="appnode.NodeRating" %>
<%@ page import="appnode.PMF" %>
<%@ page import="com.google.appengine.api.datastore.Key"%>
<%@ page import="com.google.appengine.api.datastore.KeyFactory;"%>
<html>
<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String action = request.getParameter("action");
	String keystrnode = request.getParameter("keynode");
	//String keystrnode1 = req.getParameter("keynode1");
	String nodename1 = request.getParameter("nodename1");
	
	Key key = KeyFactory.stringToKey(keystrnode);
	//Key key1 = KeyFactory.stringToKey(keystrnode1);
	String distance = request.getParameter("distance");
	Node node = pm.getObjectById(Node.class,key);
	//Node node1 = pm.getObjectById(Node.class,key1);
	Node node1 = SearchNode.getByName(nodename1);
	
	
	
		// info tab1
		out.print("<div id='tab1'>");
		if (!node.getNodename().equals(nodename1)) {
			/*out.print("<form name=form1 method=post action=msgform.jsp?fromnode="+node.getNodename()+"&tonode="+node1.getNodename()+"&typeofmsg=newmsg" + " target=myNewWin>" 
			+ "<input type=hidden name=foo value='bar'>" 
			+ "<input type=button value='Send message' onClick='sendme();'>");*/
		%>
		<p><a href="javascript:sendmessage('<%=node.getNodename()%>','<%=node1.getNodename()%>')">Send message</a></p>
		<%				
		}
		out.print("</div>");
	    // info tab1
		
	
		// info tab2
		out.print("<div id='tab2'>");
		// links
		List<Interface> links = SearchNode.getLinklist(node1);
		int sizelink = links.size();
		if (sizelink==0) {
			out.print("no links");
			out.print("<br>");
		}
		else {
			out.print("<b>links :</b> ");
			for (int j=0;j<sizelink;j++) {
				out.print("  ");
				if (node.getNodename().equals(nodename1)) {
				%>
				<a href="javascript:openwindow('editlink.jsp?keystr=<%=links.get(j).getLink()%>')"><%=links.get(j).getNode().getNodename()%></a>
				<%
				} // if
				else {
					out.print(links.get(j).getNode().getNodename());
				}
			}
				out.print("<br>");
		}
		
		// free intefaces for node1
		int sizeif = node1.getInterfaces().size();
		int sizefree = sizeif - sizelink;
		//free interfaces for node
		int sizeifnode = node.getInterfaces().size();
		int sizefreenode = sizeifnode - SearchNode.getLinklist(node).size();
		
		out.print("<b>free interfaces :</b> " + sizefree);
		out.print("<br>");
		int j = 1;
		for (int i=0;i<sizeif;i++) {
			if (node1.getInterfaces().get(i).getLink()==null || node1.getInterfaces().get(i).getLink().equals("")) {
				if (node.getNodename().equals(nodename1)) {
				%>
				<a href="javascript:openwindow('editinterface.jsp?keystrif=<%=node.getInterfaces().get(i).getKeyStr()%>')"><%=j%></a>
				
					
					<%
				}	out.print(node1.getInterfaces().get(i).getName()+","+node1.getInterfaces().get(i).getDirection()+","+node1.getInterfaces().get(i).getProtocol());
				out.print("<br>");
				j++;
			}					
		}
		if (!node1.getNodename().equals(node.getNodename()) && sizefree!=0 && sizefreenode!=0) {
		
		
			%>
			<p>
			<input type=button value="Add link" onClick="openwindow('addlink?nodename1=<%=keystrnode%>&nodename2=<%=node1.getNodename()%>&action=check');">
			<%	
			
		}
		if (node1.getNodename().equals(node.getNodename())) {
			/*out.print("<form name=form1 method=post action=addif?nodename="+node.getNodename()+"&action=new"+" target=myNewWin>" 
				+ "<input type=hidden name=foo value='bar'>" 
				+ "<input type=button value='Add interface' onClick='sendme();'>" 
				+ "</form>");*/
				%>
				<p>
				<input type=button style="padding:5px" value="Add interface" onClick="openwindow('addif?nodename=<%=node.getNodename()%>&action=new');">
					<%
				
		/*%>
		<a href="javascript:addinterface('<%=node.getNodename()%>')">Add interface</a>
		<%*/
		}
		else {
		%>
			<p><a href="javascript:showlinks('<%=node.getNodename()%>','<%=node1.getNodename()%>')">Show</a> or     
			<a href="javascript:hidelink()">Hide</a> links </p>
			<div id="showlinks"></div>
		<%
		}
	//pm.close();	
	out.print("</div>");
	 // info tab2
	
	
	// info tab3
	
	out.print("<div id='tab3'>");
		Query query = pm.newQuery(Ratings.class);
		query.setFilter("owner==nameParam");
		query.declareParameters("String nameParam");
		List<Ratings> results = (List<Ratings>) query.execute(node1.getOwner());
		
		// no ratings
		if (results.isEmpty()) {
			out.print("No ratings");
			out.print("<p></p>");	
		}
		
		else {
			DecimalFormat df = new DecimalFormat("#.#");
			Ratings rt = results.get(0);
			%>
				<table id="ratings">
					<tr>
						<td class="ratings">Communication</td>
						<td class="ratings">
							<div class="rating_bar">
					   		<div style="width:<%=20*rt.getCommun()%>%"></div>
						</div>
						</td>
						<td class="ratings">
							<%=df.format(rt.getCommun())%>
						</td>
					</tr>
					<tr>
						<td class="ratings">Collaboration</td>
						<td class="ratings">
							<div class="rating_bar">
					   		<div style="width:<%=20*rt.getCollabor()%>%"></div>
						</div>
						</td>
						<td class="ratings">
							<%=df.format(rt.getCollabor())%>
						</td>
					</tr>
					<tr>
						<td class="ratings">Expert</td>
						<td class="ratings">
							<div class="rating_bar">
					   		<div style="width:<%=20*rt.getExpert()%>%"></div>
						</div>
						</td>
						<td class="ratings">
							<%=df.format(rt.getExpert())%>
						</td>
					</tr>
				</table>
				<p>
				 <div style="padding:5px"><%=rt.getNumofrate()%> ratings</div>
				<p>
		<%			
		}
		%>
	   
		<%
		if (!node.getNodename().equals(nodename1)) {
			/*out.print("<form name=form2 method=post action=addrating.jsp?nodename=" + node.getNodename() + "&noderating=" +  + " target=myNewWin>" 
				+ "<input type=hidden name=foo value='bar'>" 
				+ "<input type=button value='Add rating' onClick='sendrating();'>" 
				+ "</form>");*/
				%>
				<input type=button value="Add rating" onClick="openwindow('addrating.jsp?nodename=<%=node.getNodename()%>&noderating=<%=node1.getNodename()%>');">
				<%

		}
	out.print("</div>");
	// info tab3
	
	//pm.close();  
%>

	
</html>